package com.wgchao.authmanager.domain.model;

import com.wgchao.authmanager.domain.params.MenuResourceCreator;
import com.wgchao.authmanager.domain.params.MenuResourceUpdator;
import com.wgchao.authmanager.domain.params.ProjectCreator;
import com.wgchao.authmanager.domain.params.ProjectUpdator;
import com.wgchao.authmanager.infrastructure.exception.IdentifyUsedException;
import com.wgchao.authmanager.infrastructure.exception.ObjectDisabledException;
import com.wgchao.authmanager.infrastructure.exception.ObjectNotExistException;
import lombok.Getter;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: Project
 * @author: wgchao
 * @createTime: 2020/4/27 9:46 AM
 * @Description: 项目
 */
@Getter
public class Project extends DomainIdentify {

    private String identify;
    private String name;
    private String remark;
    private boolean disabled;

    List<MenuResource> menuResources;

    protected Project(String identify, String name, String remark, boolean disabled) {
        this.identify = identify;
        this.name = name;
        this.remark = remark;
        this.disabled = disabled;
    }

    public Project(Long id, String identify, String name, String remark,
                   boolean disabled, List<MenuResource> menuResources, Long version) {
        setId(id);
        this.identify = identify;
        this.name = name;
        this.remark = remark;
        this.disabled = disabled;
        this.menuResources = menuResources;
        setVersion(version);
    }

    public static Project addProject(ProjectCreator creator){
        return new Project(creator.getIdentify(), creator.getName(), creator.getRemark(),
                false);
    }

    public boolean normal(){
        return !this.disabled;
    }

    public void resiveInfo(ProjectUpdator updator){
        if(!normal()){
            throw new ObjectDisabledException("项目不可用");
        }
        if(updator.getName() != null){
            this.name = updator.getName().orElse(null);
        }
        if(updator.getRemark() != null){
            this.remark = updator.getRemark().orElse(null);
        }
    }

    public void stopUsing() {
        this.disabled = true;
    }

    public void reEnable() {
        this.disabled = false;
    }

    public void addMenu(MenuResourceCreator creator){
        if(!normal()){
            throw new ObjectDisabledException("项目不可用");
        }
        if(this.menuResources==null){
            this.menuResources = new ArrayList<>();
        }
        MenuResource menuResource = findMenu(creator.getMenuIdentify());
        if(menuResource != null){
            throw new IdentifyUsedException("菜单资源表示已被使用");
        }
        this.menuResources.add(MenuResource.createMenu(creator));
    }

    public void removeMenu(String menuIdentify){
        if(!normal()){
            throw new ObjectDisabledException("项目不可用");
        }
        MenuResource menuResource = findMenu(menuIdentify);
        if(menuResource == null){
            throw new ObjectNotExistException("要删除的菜单资源不存在");
        }
        this.menuResources.remove(menuResource);
    }

    public MenuResource findMenu(String menuIdentify){
        if(this.menuResources==null){
            return null;
        }
        for(MenuResource menuResource : this.menuResources){
            if(menuResource.getMenuIdentify().equals(menuIdentify)){
                return menuResource;
            }
        }
        return null;
    }

    public void resiveMenu(String old_menuIdentify, MenuResourceUpdator updator){
        if(!normal()){
            throw new ObjectDisabledException("项目不可用");
        }
        MenuResource menuResource = findMenu(old_menuIdentify);
        if(menuResource == null){
            throw new ObjectNotExistException("要更新的菜单资源不存在");
        }
        menuResource.resiveInfo(updator);
    }
}
